package com.ericsson.enap.modules.kpi.job.ims;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * IMS单用户聚合查询 用户透明度数据获取
 * 存放在湖南EDA02（10.242.151.196，root:rootroot）的
 * /home/telorb/imssystem/目录下
 * 用户web程序ImsUserAggQueryServiceImpl getTas方法以 ssh
 * 方式连接并
 * java -cp /home/telorb/imssystem/ GetUserTasMsg param1(接口地址) param2(msidn8618640311626) param3(用户名eric) parma4(密码eric)
 *
 * MTAS1 http://10.36.51.1:8095/axis2/services/CAI3G
 *
 * MTAS2 http://10.36.39.65:8095/axis2/services/CAI3G
 *
 * MTAS3 http://10.36.53.1:8095/axis2/services/CAI3G
 *
 * MTAS4 http://10.36.42.65:8095/axis2/services/CAI3G
 */
public class GetUserTasMsg{
    public static void main(String[] args) {
        //System.out.println(args[0]+":"+args[1]+":"+args[2]+":"+args[3]);
        doTask(args[0],args[1],args[2],args[3]);
    }
    public static void doTask(String url,String msidn,String username,String password) {
            String result = login(url,username,password);
            String sessionId = result.substring(result.indexOf("<sessionId>"), result.indexOf("</sessionId>")).replace("<sessionId>", "");
            String seqId = result.substring(result.indexOf("<baseSequenceId>"), result.indexOf("</baseSequenceId>")).replace("<baseSequenceId>", "");
            get(sessionId, seqId,url,msidn);
    }

    /**
     * @浣滅敤 浣跨敤urlconnection
     * @param url
     * @param Params
     * @return
     * @throws IOException
     */
    public static String sendPost(String url, String Params) throws IOException {
        BufferedReader reader = null;
        String response = "";
        try {
            URL httpUrl = null; // HTTP URL绫� 鐢ㄨ繖涓被鏉ュ垱寤鸿繛鎺�
            // 鍒涘缓URL
            httpUrl = new URL(url);
            // 寤虹珛杩炴帴
            HttpURLConnection conn = (HttpURLConnection) httpUrl
                    .openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "text/xml;charset=UTF-8");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setUseCaches(false);// 璁剧疆涓嶈缂撳瓨
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            // POST璇锋眰
            DataOutputStream out = new DataOutputStream(conn.getOutputStream());
            out.write(Params.getBytes("UTF-8"));// **娉ㄦ剰鐐�2**锛岄渶瑕佹鏍煎紡
            out.flush();
            out.close();
            // 璇诲彇鍝嶅簲
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "utf-8"));
            String lines;
            while ((lines = reader.readLine()) != null) {
                response += lines;
            }
            reader.close();
            // 鏂紑杩炴帴
            conn.disconnect();
        } catch (Exception e) {
            System.out.println("閫� POST 璇锋眰鍑虹幇寮傚父锛�" + e);
            e.printStackTrace();
        }
        // 浣跨敤finally鍧楁潵鍏抽棴杈撳嚭娴併�佽緭鍏ユ祦
        finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return response;
    }

    public static String login(String s,String username,String password) {
        StringBuffer transferData = new StringBuffer();
        transferData.append("<soap-env:Envelope xsi:schemaLocation=\"http://schemas.xmlsoap.org/soap/envelope/../schemas/cai3g/Soap-Envelope.xsd http://schemas.ericsson.com/cai3g1.2/../schemas/cai3g/cai3g1.2_header-fault-corrected.xsd\" xmlns:cai3g=\"http://schemas.ericsson.com/cai3g1.2/\" xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
        transferData.append("<soap-env:Header/>");
        transferData.append("<soap-env:Body>");
        transferData.append("<cai3g:Login>");
        transferData.append("<cai3g:userId>");
        transferData.append(username);
        transferData.append("</cai3g:userId>");
        transferData.append("<cai3g:pwd>");
        transferData.append(password);
        transferData.append("</cai3g:pwd>");
        transferData.append("</cai3g:Login>");
        transferData.append("</soap-env:Body>");
        transferData.append("</soap-env:Envelope>");
        String rul = "";
        try {
            rul = sendPost(s,transferData.toString());
                rul = rul.replaceAll("&lt;", "<");
            //System.out.println(rul);
        } catch (IOException e) {
            System.out.println("閿欒锛�" + e.toString());
        }
        return rul;
    }

    public static String get(String sessionId, String seqId,String url,String msidn) {
        StringBuffer transferData = new StringBuffer();
        transferData.append("<soap-env:Envelope xsi:schemaLocation=\"http://schemas.xmlsoap.org/soap/envelope/ ../schemas/cai3g/Soap-Envelope.xsd http://schemas.ericsson.com/cai3g1.2/ ../schemas/cai3g/cai3g1.2_header-fault-corrected.xsd http://schemas.ericsson.com/mtas/mmtel/cai3g ../schemas/mmtel/mmtel_aggregated_service.xsd\" xmlns:cai3g=\"http://schemas.ericsson.com/cai3g1.2/\" xmlns:mc=\"http://schemas.ericsson.com/mtas/mmtel/cai3g\" xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
        transferData.append("   <soap-env:Header>");
        transferData.append("      <cai3g:SessionId>");
        transferData.append(sessionId);
        transferData.append("</cai3g:SessionId>");
        transferData.append("      <cai3g:TransactionId>");
        transferData.append(3);
        transferData.append("</cai3g:TransactionId>");
        transferData.append("      <cai3g:SequenceId>");
        transferData.append(seqId);
        transferData.append("</cai3g:SequenceId>");
        transferData.append("   </soap-env:Header>");
        transferData.append("   <soap-env:Body>");
        transferData.append("      <cai3g:Get>");
        transferData.append("         <cai3g:MOType>MMTel@http://schemas.ericsson.com/mtas/mmtel/cai3g</cai3g:MOType>");
        transferData.append("         <cai3g:MOId>");
        transferData.append("            <mc:publicId>sip:+"+msidn+"@hn.ims.mnc000.mcc460.3gppnetwork.org</mc:publicId>");
        transferData.append("         </cai3g:MOId>");
        transferData.append("      </cai3g:Get>");
        transferData.append("   </soap-env:Body>");
        transferData.append("</soap-env:Envelope>");
        String rul = "";
        try {
            rul = sendPost(
                    url,
                    transferData.toString());
            rul = rul.replaceAll("&lt;", "<");
            System.out.println(rul);
        } catch (IOException e) {
            System.out.println("error:" + e.toString());
        }
        return rul;
    }
}
